package com.service.atf.base.core;


import com.service.atf.base.model.TFInfo;
import com.luotao.devp.base.db.Pager;

import java.util.HashMap;
import java.util.Map;

/**
 * User: luotao
 * Date: 12-3-23
 * Time: 15:36:00
 */
public class TFRollReader implements Runnable {
    private int pageSize;

    private int totalCount;

    TFReader tfReader;

    ITFDBService tfdbService;

    private TFInfo currentTF;

    private Map<String, ITFReadBussProcess> processMap = new HashMap<String, ITFReadBussProcess>();

    Pager pager;

    private int readPoint;

    private int writePoint;

    TFRollAbstractController abstractTFController;

    public TFRollReader(int pageSize, int totalCount, int readPoint
            , int writePoint, TFRollAbstractController abstractTFController) {
       /* this.pageSize = pageSize;
        this.totalCount = totalCount;
        pager = Bootup.getAttribute(GlobalDefine.ATF_KEY_PAGER);
        tfdbService = Bootup.getAttribute(GlobalDefine.ATF_DB_KEY_TFINFO);
        this.readPoint = readPoint;
        this.writePoint = writePoint;
        this.abstractTFController = abstractTFController;*/
    }

  /*  public void init(XmlWapper wapper) {
      *//*  //找到最后一个读取的TF文件
        currentTF = tfdbService.getLastATF();
        while (currentTF == null) {
            ATFLogger.sysinfo("没有找到当前TF文件，" + ATFConstants.SLEEP_READ + "毫秒后再次查询当前TF文件");
            try {
                Thread.currentThread().sleep(ATFConstants.SLEEP_READ);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            currentTF = tfdbService.getLastATF();
        }
        //最新的TF文件
        TFInfo tmpTFInfo = currentTF;

        while (true) {
            tfReader = new TFReader(currentTF.getAtfFilePath(), readPoint, writePoint, pageSize, totalCount);
            tfReader.setiReadProcess(abstractTFController);
            //没有读取过此TF文件
            if (tfReader.getWriterOffset() == GlobalDefine.TF_DEFAULT_HEADER_VALUE) {
                tfReader.baseClose();
                //如果是第一个TF文件，停止
                if (currentTF.getAtfPrevCode() == 0) {
                    break;
                }
                //查找上一个文件
                currentTF = tfdbService.findById(currentTF.getAtfPrevCode());
            } else {
                break;
            }
        }
        //如果找到的TF文件不是最新的TF文件
        while (currentTF.getAtfCode() != tmpTFInfo.getAtfCode()) {
            TFReader tfReader = new TFReader(currentTF.getAtfFilePath(), readPoint, writePoint, pageSize, totalCount);
            tfReader.setiReadProcess(abstractTFController);
            //如果是找到checkpoint点的TF,则根据checkpoint点创建
            tfReader.initWithOffset(tfReader.getWriterOffset(), 0);
            while (true) {
                ResultReplyRead readResult = tfReader.readData();
                //如果读取到该文件的末尾
                if (readResult.getCode() == GlobalDefine.TF_STATUS_END) {
                    //关闭TF文件
                    tfReader.baseClose();
                    //查询下一个文件
                    currentTF = tfdbService.findById(currentTF.getAtfNextCode());
                    break;
                }
            }
        }
        //已近读取到最后一个使用的TF文件
        tfReader = new TFReader(currentTF.getAtfFilePath(), readPoint, writePoint, pageSize, totalCount);
        tfReader.setiReadProcess(abstractTFController);
        tfReader.initBody();
        while (true) {
            ResultReplyRead readResult = tfReader.readData();
            //如果追赶到ADDER，则停止循环
            if (readResult.getCode() == GlobalDefine.TF_STATUS_POINT_EQUALS
                    || readResult.getCode() == GlobalDefine.TF_STATUS_ADDER_EMPTY) {
                break;
            }
        }
        ATFLogger.sysinfo("开始启动读取文件线程");
        Thread t = new Thread(this);
        t.start();
        ATFLogger.sysinfo("DB1启动完成");
        *//**//*   ThreadConfig config=new ThreadConfig("FILE_FORCE");
       config.setDeamon(true).setSpeed(3000);
       tdhandle=ThreadCenter.startup(this, config);*//*
    }*/


   // @Override
    public void run() {
      /*  while (true) {
            try {
                Thread.currentThread().sleep(ATFConstants.SLEEP_READ);
                ResultReplyRead readResult = tfReader.readData();
                //如果读取到文件的末尾，则查询是否有下一个文件，否则，等待
                if (readResult.getCode() == GlobalDefine.TF_STATUS_END) {
                    tfReader.baseClose();
                    TFInfo temp = tfdbService.findById(currentTF.getAtfCode());
                    if (temp.getAtfNextCode() == 0) {
                        continue;
                    }

                    //查询下个TF文件的信息
                    TFInfo next = tfdbService.findById(temp.getAtfNextCode());
                    if (next == null) {
                        continue;
                    }
                    tfReader = new TFReader(next.getAtfFilePath(), readPoint, writePoint, pageSize, totalCount);
                    tfReader.setiReadProcess(abstractTFController);
                }
                //如果读指针追赶到写指针或者TF文件是空
                else if (readResult.getCode() == GlobalDefine.TF_STATUS_POINT_EQUALS
                        || readResult.getCode() == GlobalDefine.TF_STATUS_ADDER_EMPTY) {
                    continue;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }*/
    }
}
